<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="root">
        <button @click="handleClick('home')">主页</button>
        <button @click="handleClick('user')">用户</button>
        <!-- <home></home>
        <user></user> -->
        <keep-alive :max="1">
            <component :is="componentId"></component>
        </keep-alive>
    </div>

    <script src="./vue.js"></script>

    <script>
        Vue.component("home",{
            template:`
                <div>主页</div>
            `,
            destroyed() {
                console.log("home 销毁的时候");
            },
        })
        let vm1 = new Vue({
            el:"#root",
            data:{
                componentId:"home"
            },
            components:{
                user:{
                    template:`
                        <div>用户</div>
                    `,
                    destroyed(){
                        console.log("user 组件销毁了");
                    }
                }
            },
            methods: {
                handleClick:function(compName){
                    // console.log(compName);
                    this.componentId = compName;
                }
            },
        })

    </script>
</body>
</html>